package au.com.twosquared.animalfarm.sprites;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

// Just a wrapper, I can't stand working with something called textureregion though...
/**
 * Loads and manages images
 * 
 * @author Jan Martin <jan.martin@twosquared.com.au>
 * @version 1.0 2011-15-11
 * @since 2011-15-11
 * 
 */
public class Image {

	/**
	 * Texture used for images and mapping
	 */
	TextureRegion texture;

	/**
	 * Creates a new texture given a texture
	 * 
	 * @param texture
	 *            given texture
	 */
	public Image(TextureRegion texture) {
		this.texture = texture;
	}

	/**
	 * Returns the width of a texture
	 * 
	 * @return the region width
	 */
	public int getWidth() {
		return texture.getRegionWidth();
	}

	/**
	 * Returns the height of a texture
	 * 
	 * @return the region height
	 */
	public int getHeight() {
		return texture.getRegionHeight();
	}

	/**
	 * Returns the texture
	 * 
	 * @return the texture
	 */
	public TextureRegion image() {
		return texture;
	}

	/**
	 * Loads an image given a name
	 * 
	 * @param name
	 *            the name of the image
	 * @return the image
	 */
	public static Image load(String name) {
		TextureRegion region = new TextureRegion(new Texture(
				Gdx.files.internal(name)));
		return new Image(region);
	}

	/**
	 * Loads an image given a name and dimensions
	 * 
	 * Used for loading textures which are not pow2
	 * 
	 * @param name
	 *            the name of the image
	 * @param width
	 *            the width of the desired region
	 * @param height
	 *            the height of the desired region
	 * @return the image
	 */
	public static Image load(String name, int width, int height) {
		TextureRegion region = new TextureRegion(new Texture(
				Gdx.files.internal(name)), 0, 0, width, height);
		return new Image(region);
	}

	/**
	 * Loads an animation strip
	 * 
	 * @param name
	 *            the name of the images
	 * @param frames
	 *            the number of frames
	 * @param width
	 *            width of each frame
	 * @param height
	 *            height of each frame
	 * @return a sprite
	 * 
	 * @Jan I would have preferred to not have to use width or height paramater
	 *      but due to the base2 limitation of opengl I can't think of any other
	 *      way to do it :( if someone can come up with one I would very much
	 *      like to hear it
	 */
	public static Image[] load(String name, int frames, int width, int height) {
		Image[] sprites = new Image[frames];
		Texture texture = new Texture(Gdx.files.internal(name));
		// int w = texture.getWidth() / frames;
		// int h = texture.getHeight();
		for (int i = 0; i < frames; i++) {
			TextureRegion region = new TextureRegion(texture, i * width, 0,
					width, height);
			sprites[i] = new Image(region);
		}
		return sprites;
	}

}
